package com.blockmeta.bbs.businesslibrary.widget.custom;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatRadioButton;
import android.util.AttributeSet;

import com.blockmeta.bbs.baselibrary.R;
import com.blockmeta.bbs.businesslibrary.arouter.RouterHelper;
import com.blockmeta.bbs.businesslibrary.arouter.RouterPath;
import com.blockmeta.bbs.businesslibrary.util.AccountHelper;

/**
 * @author AirThor
 * @date 2020/4/22.
 */
public class DrawableRadioButton extends AppCompatRadioButton {
    private int mDrawableBoundWidth,mDrawableBoundHeight;
    public boolean isNeedLogin;
    public DrawableRadioButton(Context context) {
        super(context);
    }

    public DrawableRadioButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        Drawable left,top,right,bottom;
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.DrawableRadioButton);
        left = typedArray.getDrawable(R.styleable.DrawableRadioButton_leftDrawable);
        top = typedArray.getDrawable(R.styleable.DrawableRadioButton_topDrawable);
        right = typedArray.getDrawable(R.styleable.DrawableRadioButton_rightDrawable);
        bottom = typedArray.getDrawable(R.styleable.DrawableRadioButton_bottomDrawable);
        mDrawableBoundWidth = (int) typedArray.getDimension(R.styleable.DrawableRadioButton_drawableBoundWidth,0f);
        mDrawableBoundHeight = (int) typedArray.getDimension(R.styleable.DrawableRadioButton_drawableBoundHeight,0f);
        isNeedLogin = typedArray.getBoolean(R.styleable.DrawableRadioButton_needLogin,false);
        typedArray.recycle();

        setCompoundDrawablesRelativeWithIntrinsicBounds(left,top,right,bottom);
    }

    @Override
    public void setCompoundDrawablesRelativeWithIntrinsicBounds(@Nullable Drawable left, @Nullable Drawable top, @Nullable Drawable right, @Nullable Drawable bottom) {
        if (left != null) {
            left.setBounds(0, 0, mDrawableBoundWidth==0?left.getIntrinsicWidth():mDrawableBoundWidth, mDrawableBoundHeight==0?left.getIntrinsicHeight():mDrawableBoundHeight);
        }
        if (right != null) {
            right.setBounds(0, 0, mDrawableBoundWidth==0?right.getIntrinsicWidth():mDrawableBoundWidth, mDrawableBoundHeight==0?right.getIntrinsicHeight():mDrawableBoundHeight);
        }
        if (top != null) {
            top.setBounds(0, 0, mDrawableBoundWidth==0?top.getIntrinsicWidth():mDrawableBoundWidth, mDrawableBoundHeight==0?top.getIntrinsicHeight():mDrawableBoundHeight);
        }
        if (bottom != null) {
            bottom.setBounds(0, 0, mDrawableBoundWidth==0?bottom.getIntrinsicWidth():mDrawableBoundWidth, mDrawableBoundHeight==0?bottom.getIntrinsicHeight():mDrawableBoundHeight);
        }
        setCompoundDrawables(left, top, right, bottom);
    }

    @Override
    public void toggle() {
        if (isNeedLogin&&toLoginIfUnLogin()){
            return;
        }
        super.toggle();
    }


    /**
     * 未登录则跳转登陆
     */
    public boolean toLoginIfUnLogin(){
        boolean unLogin = !AccountHelper.isLogin();
        if (unLogin) {
            RouterHelper.startActivity(getContext(), RouterPath.ACCOUNT_LOGIN_ACTIVITY);
        }
        return unLogin;
    }
}
